﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Bombardiman
{
    public class Animation
    {
        // The image representing the collection of images used for animation
        private Texture2D spriteStrip;
        // The scale used to display the sprite strip
        private float scale;
        // The time since we last updated the frame
        private int elapsedTime;
        // The time we display a frame until the next one
        private int frameTime;
        // The number of frames that the animation contains
        private int frameCount;
        // The index of the current frame we are displaying
        private int currentFrame;
        // The color of the frame we will be displaying
        private Color color;
        // The area of the image strip we want to display
        private Rectangle sourceRect = new Rectangle();
        // The area where we want to display the image strip in the game
        private Rectangle destinationRect = new Rectangle();
        // Width of a given frame
        private int FrameWidth;
        // Height of a given frame
        private int FrameHeight;
        // The state of the Animation
        private bool Active;
        // Determines if the animation will keep playing or deactivate after one run
        private bool Looping;
        // Width of a given frame
        private Vector2 Position;
        //
        private bool animating;
        private int direction;

        public Animation(int frameCount, int frametime, Color color, float scale, bool looping, int direction, bool animating)
        {
            this.frameCount = frameCount;
            this.frameTime = frametime;
            this.scale = scale;
            this.color = color;
            Looping = looping;
            this.direction = direction;
            this.animating = animating;
        }

        public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight)
        {
            // Keep a local copy of the values passed in
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
           
            Position = position;
            spriteStrip = texture;

            // Set the time to zero
            elapsedTime = 0;
            currentFrame = 0;

            // Set the Animation to active by default
            Active = true;
        }
        public void Update(GameTime gameTime)
        {
            // Do not update the game if we are not active
            if (Active == false)
                return;

            if (animating)
            {
                // Update the elapsed time
                elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
                // If the elapsed time is larger than the frame time
                // we need to switch frames
                if (elapsedTime > frameTime)
                {
                    // Move to the next frame
                    currentFrame++;
                    // If the currentFrame is equal to frameCount reset currentFrame to zero
                    if (currentFrame == frameCount)
                    {
                        currentFrame = 0;
                        // If we are not looping deactivate the animation
                        if (Looping == false)
                        {
                            Active = false;
                            animating = false;
                        }
                    }
                    // Reset the elapsed time to zero
                    elapsedTime = 0;
                }
            }

            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
            sourceRect = new Rectangle(currentFrame * FrameWidth, (direction * FrameHeight), FrameWidth, FrameHeight);

            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
            destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2,
            (int)Position.Y - (int)(FrameHeight * scale) / 2,
            (int)(FrameWidth * scale),
            (int)(FrameHeight * scale));
        }
        // Draw the Animation Strip
        public void Draw(SpriteBatch spriteBatch)
        {
            // Only draw the animation when we are active
            if (Active)
            {
                spriteBatch.Draw(spriteStrip, Position, sourceRect, Color.White, 0,
                    new Vector2(0, 0), 1f, SpriteEffects.None, 0);
            }
        }
        public void setPosition(Vector2 Position)
        {
            this.Position = Position;
        }

        public void setAnimating(bool animating)
        {
            this.animating = animating;
        }
        public bool isAnimating()
        {
            return animating;
        }
        public void setDirection(int direction)
        {
            this.direction = direction;
        }
        public int getDirection()
        {
            return direction;
        }
    }
}
